﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using ZLDC.Common.Estate;

namespace ZLDC.Estate.TM
{
    public partial class Cdepartment : _PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Page.Request.QueryString["tid"] == null || Page.Request.QueryString["tid"].Trim() == string.Empty)
                {
                    Page.Response.Write("提交的参数不够!");
                    Page.Response.End();
                }
                else
                {
                    ViewState["t_id"] = _Convert.ToInt(Page.Request.QueryString["tid"], 0);
                }
                
                int deptId = _Convert.ToInt(ViewState["t_id"].ToString(), 0);
                ltlDeptName.Text = ZLDC.DAL.Estate.DeptDAL.GetDeptNameById(deptId);
                
                LoadUsage();    // 初始化部门的楼盘用途
                LoadGVArea();   // 加载选中部门的授权片区
                LoadGVEstate(); // 加载选中部门的授权楼盘   
                btnCompetences();   // 权限控制按钮
            }               
        }

        // 权限控制按钮
        private void btnCompetences()
        {
            int deptId = _Convert.ToInt(ViewState["t_id"].ToString(), 0);

            DataTable dt = ZLDC.DAL.Estate.DeptDAL.getDeptInfo(deptId);
            if (dt != null && dt.Rows.Count > 0)
            {
                string type = dt.Rows[0]["type"].ToString();

                if (type.Trim() == "区域" || type.Trim() == "营业部")
                {
                    this.cbxAreaCust.Visible = true;

                    // mark1=1 表示下级开放下级所有部门公客
                    int mark1 = ZLDC.Topo.BLL.Estate.AuthorizationBLL.GetMark1FromAuthorization(deptId);
                    if (mark1 > 0)
                        this.cbxAreaCust.Checked = true;
                    else
                        this.cbxAreaCust.Checked = false;
                }
                else
                {
                    this.cbxAreaCust.Visible = false;
                    this.cbxAreaCust.Checked = false;
                }
            }            
        }

        // 初始化部门的楼盘用途
        private void LoadUsage()
        {
            int deptId = _Convert.ToInt(ViewState["t_id"].ToString(), 0);

            //读取楼盘用途
            DataTable dt = ZLDC.Topo.BLL.Estate.SystemParamBLL.ListByType("用途");
            DataView dvUsage = new DataView(dt);

            //情空原先的楼盘用途
            ListUsage.Items.Clear();

            //填充楼盘用途
            foreach (DataRowView Row in dvUsage)
            {
                string strTemp = Row["item"].ToString();
                ListUsage.Items.Add(strTemp);
            }

            // 当前部门已选范围
            ZLDC.Topo.Model.Estate.AuthorizationInfo info = ZLDC.Topo.BLL.Estate.AuthorizationBLL.p_authorization_detail(deptId);
            string usage = info.Useage;

            if (!string.IsNullOrEmpty(usage))
            {
                // 根据输入片区列表中的数据更新列表复选框               
                foreach (ListItem li in ListUsage.Items)
                {
                    string[] strArr = usage.Split(',');
                    for (int i = 0; i < strArr.Length; i++)
                    {
                        if (strArr[i].ToString()==ReturnUse(li.Text))
                            li.Selected = true;
                    }

                    
                }
            }
        }

        // 加载选中部门的授权片区
        private void LoadGVArea()
        {
            int deptId = _Convert.ToInt(ViewState["t_id"].ToString(), 0);

            DataTable dt = ZLDC.Topo.BLL.Estate.AuthorizationBLL.p_authorization_detail_table(1, deptId);

            gvArea.DataSource = dt;
            gvArea.DataKeyNames = new string[] { "areaid" };//主键 
            gvArea.DataBind();
        }

        // 加载选中部门的授权楼盘
        private void LoadGVEstate()
        {
            int deptId = _Convert.ToInt(ViewState["t_id"].ToString(), 0);

            DataTable dt = ZLDC.Topo.BLL.Estate.AuthorizationBLL.p_authorization_detail_table(2, deptId);


            gvEstate.DataSource = dt;
            gvEstate.DataKeyNames = new string[] { "estateid" };//主键 
            gvEstate.DataBind();
        }

        // 授权片区删除
        protected void gvArea_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int deptId = _Convert.ToInt(ViewState["t_id"].ToString(), 0);
            string areaid = gvArea.DataKeys[e.RowIndex].Value.ToString();

            ZLDC.Topo.BLL.Estate.AuthorizationBLL.p_authorization_delete(1, deptId, areaid);
            
            LoadGVArea();
        }

        // 授权片区行梆定时
        protected void gvArea_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //如果是绑定数据行 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标经过时，行背景色变 
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
                //鼠标移出时，行背景色变 
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");                

                DataRowView drv = (DataRowView)e.Row.DataItem;
                DataRow dr = drv.Row;

                CheckBox cbIsAreaTel = (CheckBox)e.Row.Cells[2].Controls[0].FindControl("cbIsAreaTel");
                if (dr["isAreaTel"].ToString() == "1")
                    cbIsAreaTel.Checked = true;

                CheckBox cbIsAreaFollow = (CheckBox)e.Row.Cells[3].Controls[0].FindControl("cbIsAreaFollow");
                if (dr["isAreaFollow"].ToString() == "1")
                    cbIsAreaFollow.Checked = true;

                CheckBox cbIsAreaInfo = (CheckBox)e.Row.Cells[4].Controls[0].FindControl("cbIsAreaInfo");
                if (dr["isAreaInfo"].ToString() == "1")
                    cbIsAreaInfo.Checked = true;

                CheckBox cbIsCusTel = (CheckBox)e.Row.Cells[5].Controls[0].FindControl("cbIsCusTel");
                if (dr["isCusTel"].ToString() == "1")
                    cbIsCusTel.Checked = true;

                CheckBox cbIsCusFollow = (CheckBox)e.Row.Cells[6].Controls[0].FindControl("cbIsCusFollow");
                if (dr["isCusFollow"].ToString() == "1")
                    cbIsCusFollow.Checked = true;
            }

        }

        // 授权片区翻页
        protected void gvArea_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {            
            gvArea.PageIndex = e.NewPageIndex;
            LoadGVArea();
        }

        // 授权楼盘删除
        protected void gvEstate_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int deptId = _Convert.ToInt(ViewState["t_id"].ToString(), 0);
            string estateid = gvEstate.DataKeys[e.RowIndex].Value.ToString();

            ZLDC.Topo.BLL.Estate.AuthorizationBLL.p_authorization_delete(2, deptId, estateid);

            LoadGVEstate(); 
        }

        // 授权楼盘行梆定时
        protected void gvEstate_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //如果是绑定数据行 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标经过时，行背景色变 
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
                //鼠标移出时，行背景色变 
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");

                DataRowView drv = (DataRowView)e.Row.DataItem;
                DataRow dr = drv.Row;

                CheckBox cbIsEstateTel = (CheckBox)e.Row.Cells[2].Controls[0].FindControl("cbIsEstateTel");
                if (dr["isEstateTel"].ToString() == "1")
                    cbIsEstateTel.Checked = true;

                CheckBox cbIsEstateFollow = (CheckBox)e.Row.Cells[3].Controls[0].FindControl("cbIsEstateFollow");
                if (dr["isEstateFollow"].ToString() == "1")
                    cbIsEstateFollow.Checked = true;

                CheckBox cbIsEstateInfo = (CheckBox)e.Row.Cells[4].Controls[0].FindControl("cbIsEstateInfo");
                if (dr["isEstateInfo"].ToString() == "1")
                    cbIsEstateInfo.Checked = true;
            }

        }

        // 授权楼盘翻页
        protected void gvEstate_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvEstate.PageIndex = e.NewPageIndex;
            LoadGVEstate();
        }

        // 提交确定
        protected void imgBtnOK_Click(object sender, ImageClickEventArgs e)
        {
            int deptId = _Convert.ToInt(ViewState["t_id"].ToString(), 0);

            string dept = ltlDeptName.Text;         
            string useage = "";     // 业务范围
            //string areas = "";
            //string areasTel = "";
            //string areasFollow = "";
            //string areasInfo = "";
            //string estates = "";
            //string estatesTel = "";
            //string estatesFollow = "";
            //string estatesInfo = "";
            //string cusTel = "";
            //string cusFollow = "";



            //for (int i = 0; i < gvArea.Rows.Count; i++)
            //{
            //    string area = gvArea.Rows[i].Cells[0].Text;
            //    areas += area + ",";

            //    CheckBox cbIsAreaTel = (CheckBox)gvArea.Rows[i].Cells[2].Controls[0].FindControl("cbIsAreaTel");
            //    if (cbIsAreaTel.Checked)
            //        areasTel += area + ",";

            //    CheckBox cbIsAreaFollow = (CheckBox)gvArea.Rows[i].Cells[3].Controls[0].FindControl("cbIsAreaFollow");
            //    if (cbIsAreaFollow.Checked)
            //        areasFollow += area + ",";

            //    CheckBox cbIsAreaInfo = (CheckBox)gvArea.Rows[i].Cells[4].Controls[0].FindControl("cbIsAreaInfo");
            //    if (cbIsAreaInfo.Checked)
            //        areasInfo += area + ",";

            //    CheckBox cbIsCusTel = (CheckBox)gvArea.Rows[i].Cells[5].Controls[0].FindControl("cbIsCusTel");
            //    if (cbIsCusTel.Checked)
            //        cusTel += area + ",";

            //    CheckBox cbIsCusFollow = (CheckBox)gvArea.Rows[i].Cells[6].Controls[0].FindControl("cbIsCusFollow");
            //    if (cbIsCusFollow.Checked)
            //        cusFollow += area + ",";
            //}

            //for (int i = 0; i < gvEstate.Rows.Count; i++)
            //{
            //    string estate = gvEstate.Rows[i].Cells[0].Text;
            //    estates += estate + ",";

            //    CheckBox cbIsEstateTel = (CheckBox)gvEstate.Rows[i].Cells[2].Controls[0].FindControl("cbIsEstateTel");
            //    if (cbIsEstateTel.Checked)
            //        estatesTel += estate + ",";

            //    CheckBox cbIsEstateFollow = (CheckBox)gvEstate.Rows[i].Cells[3].Controls[0].FindControl("cbIsEstateFollow");
            //    if (cbIsEstateFollow.Checked)
            //        estatesFollow += estate + ",";

            //    CheckBox cbIsEstateInfo = (CheckBox)gvEstate.Rows[i].Cells[4].Controls[0].FindControl("cbIsEstateInfo");
            //    if (cbIsEstateInfo.Checked)
            //        estatesInfo += estate + ",";
            //}
            
            foreach (ListItem li in ListUsage.Items)
            {
                if (li.Selected)
                {                              
                    useage += ReturnUse(li.Text) + ",";
                }
            }

            string mark1 = "0";     // 1区域开放所有公客 2整个营业部内开放
            DataTable dt = ZLDC.DAL.Estate.DeptDAL.getDeptInfo(deptId);
            if (dt != null && dt.Rows.Count > 0)
            {
                string type = dt.Rows[0]["type"].ToString();

                if (type.Trim() == "区域" && cbxAreaCust.Checked)
                    mark1 = "1";
                else if (type.Trim() == "营业部" && cbxAreaCust.Checked)
                    mark1 = "2";
            }

            if (useage.LastIndexOf(',') > 0)
                useage = useage.Substring(0, useage.Length - 1);

           /// ZLDC.Topo.BLL.Estate.AuthorizationBLL.p_authorization_save(deptId, dept, useage, areas, areasTel, areasFollow, areasInfo, estates, estatesTel, estatesFollow, estatesInfo, cusTel, cusFollow, mark1);


            ZLDC.Topo.BLL.Estate.AuthorizationBLL.p_saveusage(deptId, dept, useage,mark1);

            _JavaScript.Alert(this.Page, "保存成功！");

            ///更新片区授权，清空缓存信息
            _Cache.Remove("GetAreaByDeptId_AndUse_" + deptId);
            _Cache.Remove("GetAreaByDeptId_" + deptId);
            _Cache.Remove("GetNoAreaByDeptId_" + deptId);


            return;
        }


        //// 通过输入部门ID获取部门片区权限列表 参数：nId	输入参数  部门IDstrAreaList 输出参数  部门片区权限列表(如："1,2,3,6")
        //private void GetDepAreaList(int nId, out string strAreaList)
        //{
        //    //查询部门表
        //    string strSql = "select AreaList,name from v_bi_dept where Id = " + Convert.ToString(nId);
        //    DataSet dsDep = null;
        //    using (_Database db = new _Database("estate"))
        //    {
        //        dsDep = db.Query(strSql);
        //    }
        //    //取得部门片区权限
        //    DataView dvTree = new DataView(dsDep.Tables[0]);
        //    strAreaList = dvTree[0].Row["AreaList"].ToString();

        //    //当前部门名称
        //    this.ltlDeptName.Text = dvTree[0].Row["name"].ToString();
        //}

        //private DataSet dsDep;

        //#region Web 窗体设计器生成的代码
        //override protected void OnInit(EventArgs e)
        //{
        //    InitializeComponent();
        //    base.OnInit(e);
        //}

        ///// <summary>
        ///// 设计器支持所需的方法 - 不要使用代码修改器修改
        ///// 此方法的内容。
        ///// </summary>
        //private void InitializeComponent()
        //{
        //    this.BtnSaveDep.Click += new System.Web.UI.ImageClickEventHandler(this.BtnSaveDep_Click);
        //}
        //#endregion

        /*
        //是否是可操作部门
        private bool checkDeptInSonList(string dpid)
        {
            dpid = "," + dpid + ",";
            string dplist = "";// "," + Lpgl.LM.Business.CurrentUser.DeptIdList + ",";
            if (dplist.IndexOf(dpid) > -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        // 根据输入片区设置片区列表中的数据和复选框       
        private void InitListArea()
        {
            ListArea.SessionID = Session.SessionID;
            ListArea.AllowPaging = false;
            ListArea.AllowSorting = false;
            ListArea.CheckboxPost = false;
            ListArea.ColumnLengthInc = 40;
            ListArea.OrderField = "d_id";
            ListArea.SortDirection = "asc";

            //读取片区记录
            string strSql = "select d_Id,Id,dname,name from v_bi_area where DltFlag = 0";
            ListArea.SQL = strSql;
            ListArea.IDStyle = "checkbox";
            ListArea.BuildDataGrid();
            ListArea.SetColumnInvisible("id,d_Id");

            //生成的表头如下
            //复选框	d_Id城区ID(隐藏)  id片区ID(隐藏) dname城区名字 name片区名字
        }

        // 设置当前选择部门的片区列表复选框        
        private void UpdateCurDepListArea()
        {
            //定义当前部门ID
            int nId = 0;

            //定义部门片区权限
            string strAreaList;

            //获取当前部门ID
            GetSelecteDep(out nId);

            //取得部门片区权限
            GetDepAreaList(nId, out strAreaList);

            //定义部门拥有权限的片区数组
            ArrayList naAreaId = new ArrayList();

            //分解部门片区权限字符串
            ConvertStrList(strAreaList, ref naAreaId);

            //更新片区复选框
            UpdateListArea(naAreaId);
        }

        // 设置当前选择部门的楼盘用途复选框      
        private void UpdateCurDepListUsage()
        {
            //定义当前部门ID
            int nId = 0;

            //定义部门楼盘用途权限
            string strUsageList;

            //获取当前部门ID
            GetSelecteDep(out nId);

            //如果没有选择的部门则直接返回
            if (nId == -1)
                return;

            //取得部门楼盘用途权限
            GetDepUsageList(nId, out strUsageList);

            //定义部门拥有权限的楼盘用途数组
            ArrayList naUsage = new ArrayList();

            //分解部门楼盘用途权限字符串
            ConvertStrList(strUsageList, ref naUsage);

            //更新楼盘用途复选框
            UpdateListUsage(naUsage);
        }

        // 根据输入片区列表中的数据更新列表复选框       
        private void UpdateListArea(ArrayList naAreaId)
        {
            int nItemCount = ListArea.Items.Count;
            for (int i = 0; i < nItemCount; i++)
            {
                bool bCheck = false;
                for (int j = 0; j < naAreaId.Count; j++)
                {
                    string str1 = naAreaId[j].ToString();
                    string str2 = ListArea.Items[i].Cells[1].Text;
                    if (naAreaId[j].ToString() == ListArea.Items[i].Cells[1].Text)
                    {
                        bCheck = true;
                        break;
                    }
                }
                CheckBox selection = (CheckBox)ListArea.Items[i].Cells[0].FindControl("ctrl");
                selection.Checked = bCheck;
            }
        }

        // 根据片区列表复选框得到片区Id数组       
        private void GetSelListArea(ref ArrayList naAreaId)
        {
            naAreaId.Clear();
            int nItemCount = ListArea.Items.Count;
            for (int i = 0; i < nItemCount; i++)
            {
                CheckBox ck1 = (CheckBox)ListArea.Items[i].Cells[0].FindControl("ctrl");
                if (ck1.Checked == true)
                {
                    string strTemp = ListArea.Items[i].Cells[1].Text;
                    naAreaId.Add(strTemp);
                }
            }
        }

        //初始化部门的楼盘用途
        private void InitListUsage()
        {
            //读取楼盘用途
            DataTable dt = ZLDC.Topo.BLL.Estate.SystemParamBLL.ListByType("用途");
            DataView dvUsage = new DataView(dt);

            //情空原先的楼盘用途
            ListUsage.Items.Clear();

            //填充楼盘用途
            foreach (DataRowView Row in dvUsage)
            {
                string strTemp = Row["item"].ToString();
                ListUsage.Items.Add(strTemp);
            }
        }

        // 根据楼盘用途列表复选框得到楼盘用途字符串        
        private void GetSelListUsage(out string strUsage)
        {
            string strTemp = "";
            foreach (ListItem li in ListUsage.Items)
            {
                if (li.Selected)
                {
                    if (strTemp != "")
                        strTemp += ",";
                    strTemp += li.Text;
                }
            }
            strUsage = strTemp;
        }

        // 获取当前部门树形视图选择的部门ID 参数：nId 输出参数  部门ID
        private void GetSelecteDep(out int nId)
        {
            nId = Convert.ToInt32(ViewState["t_id"]);
        }

        // 通过输入部门ID获取部门片区权限列表 参数：nId	输入参数  部门IDstrAreaList 输出参数  部门片区权限列表(如："1,2,3,6")
        private void GetDepAreaList(int nId, out string strAreaList)
        {
            //查询部门表
            string strSql = "select AreaList,name from v_bi_dept where Id = " + Convert.ToString(nId);

            using (_Database db = new _Database("estate"))
            {
                dsDep = db.Query(strSql);
            }
            //取得部门片区权限
            DataView dvTree = new DataView(dsDep.Tables[0]);
            strAreaList = dvTree[0].Row["AreaList"].ToString();

            //当前部门名称
            this.ltlDeptName.Text = dvTree[0].Row["name"].ToString();
        }

        // 功能：设置部门的片区权限列表 参数：nId 输入参数  部门ID strAreaList 输入参数  部门片区权限列表(如："1,2,3,6")
        private void SetDepAreaList(int nId, string strAreaList)
        {
            //更新部门表
            string strSql = "if exists(select id from t_bi_dept where id = " + Convert.ToString(nId) + ")";
            strSql += " begin";
            strSql += " update t_bi_Dept set AreaList = '" + strAreaList + "' where Id = " + Convert.ToString(nId) + ";";
            strSql += " end";
            strSql += " else";
            strSql += " begin";
            strSql += " insert into t_bi_dept (usagelist,arealist,id) values ('','" + strAreaList + "'," + Convert.ToString(nId) + ");";
            strSql += " end";

            using (_Database db = new _Database("estate"))
            {
                db.ExecuteSql(strSql);
            }
        }

        // 通过输入部门ID获取部门楼盘用途权限列表 参数：nId	输入参数  部门ID  strAreaList 输出参数  部门楼盘用途权限列表(如："住宅,仓库,厂房,商铺")
        private void GetDepUsageList(int nId, out string strUsageList)
        {
            //查询部门表
            string strSql = "select UsageList,AreaCust,type,luxurious,ip,LuxTelEstate from v_bi_dept where Id = " + Convert.ToString(nId);

            using (_Database db = new _Database("estate"))
            {
                dsDep = db.Query(strSql);
            }

            //取得部门楼盘用途权限
            DataView dvDep = new DataView(dsDep.Tables[0]);
            strUsageList = dvDep[0].Row["UsageList"].ToString();
            //
            if (dvDep[0].Row["type"].ToString().Trim() == "分行")
            {
                this.pnIP.Visible = true;
            }
            else
            {
                this.pnIP.Visible = false;
            }
            //同区域开放公客
            //if (dvDep[0].Row["type"].ToString().Trim() == "区域" || dvDep[0].Row["type"].ToString().Trim() == "分行")
            if (dvDep[0].Row["type"].ToString().Trim() == "区域" || dvDep[0].Row["type"].ToString().Trim() == "营业部")
            {
                this.cbxAreaCust.Visible = true;
                if (dvDep[0].Row["AreaCust"].ToString().Trim() == "1")
                {
                    this.cbxAreaCust.Checked = true;
                }
                else
                {
                    this.cbxAreaCust.Checked = false;
                }
            }
            else
            {
                this.cbxAreaCust.Visible = false;
                this.cbxAreaCust.Checked = false;
            }
            //if (Lpgl.LM.Business.CurrentUser.RoleName != "系统管理员")
            //{
            //    //控制区经 营运副总监 能操作开放公客
            //    if (Convert.ToBoolean(ViewState["in"]) == true)
            //    {
            //        this.cbxAreaCust.Enabled = true;
            //        if (this.cbxAreaCust.Visible == true)//公客控制可见
            //        {
            //            this.BtnSaveDep.Visible = true;
            //        }
            //    }
            //    else
            //    {
            //        this.cbxAreaCust.Enabled = false;
            //        this.BtnSaveDep.Visible = false;
            //    }
            //}
            //豪宅
            if (dvDep[0].Row["luxurious"].ToString().Trim() == "1")
            {
                this.cbxLuxurious.Checked = true;
            }
            else
            {
                this.cbxLuxurious.Checked = false;
            }
            //ip
            this.txtIP.Text = dvDep[0].Row["ip"].ToString().Trim();
            //
            BindLuxTelEstate(dvDep[0].Row["LuxTelEstate"].ToString().Trim());
        }

        // 功能：设置部门的部门楼盘用途权限列表  参数：	nId	输入参数  部门ID strUsageList 输入参数  部门部门楼盘用途权限列表(如："住宅,仓库,厂房,商铺")
        private void SetDepUsageList(int nId, string strUsageList)
        {
            //更新部门表
            string strSql = "if exists(select id from t_bi_dept where id = " + Convert.ToString(nId) + ")";
            strSql += " begin";
            strSql += " update t_bi_Dept set UsageList = '" + strUsageList + "' where Id = " + Convert.ToString(nId) + ";";
            strSql += " end";
            strSql += " else";
            strSql += " begin";
            strSql += " insert into t_bi_dept (usagelist,arealist,id) values ('" + strUsageList + "',''," + Convert.ToString(nId) + ");";
            strSql += " end";

            using (_Database db = new _Database("estate"))
            {
                db.ExecuteSql(strSql);
            }
        }

        // 功能：根据输入楼盘用途更新楼盘用途复选框 参数：naUsage	输入参数  楼盘用途数组
        private void UpdateListUsage(ArrayList naUsage)
        {
            foreach (ListItem li in ListUsage.Items)
            {
                bool bCheck = false;
                for (int j = 0; j < naUsage.Count; j++)
                {
                    string str1 = naUsage[j].ToString();
                    string str2 = li.Text;
                    if (str1 == str2)
                    {
                        bCheck = true;
                        break;
                    }
                }
                li.Selected = bCheck;
            }
        }

        // 更新 开放同区域公客   查看豪宅权限 
        private void UpdateAreaCust(int iDept, bool bAreaCust)
        {
            string strSql = "update t_bi_dept set AreaCust = " + (bAreaCust == true ? "1" : "0") + ",Luxurious = " + (this.cbxLuxurious.Checked == true ? "1" : "0") + ",ip = " + (this.txtIP.Text.Trim()) + ",LuxTelEstate = " + (hiddLuxTelEstateList.Value) + " where id = " + iDept.ToString();

            using (_Database db = new _Database("estate"))
            {
                db.ExecuteSql(strSql);
            }
        }

        // 分解输入的字符串得到匹配的单个字符串 参数：strList 输入参数  列表(如："1,2,3,6")  naItem	输出参数  匹配数组     
        private void ConvertStrList(string strList, ref ArrayList naItem)
        {
            //清空数组
            naItem.Clear();

            //定义临时字符串
            string[] strTemp = strList.Split(',');

            //设置输出结果
            for (int i = 0; i < strTemp.Length; i++)
            {
                if (strTemp[i].Trim() != string.Empty)
                {
                    naItem.Add(strTemp[i].Trim());
                }
            }
        }

        // 功能：将选择片区ID生成部门片区权限字符串  参数：naAreaId	输入参数  片区Id数组 strAreaList 输出参数  部门片区权限列表(如："1,2,3,6")
        private void ReconvertAreaList(out string strAreaList, ArrayList naAreaId)
        {
            strAreaList = "";
            string strTemp = "";
            int nItemCount = naAreaId.Count;
            for (int i = 0; i < nItemCount; i++)
            {
                if (strTemp != "")
                    strTemp += ",";
                strTemp += naAreaId[i].ToString();
            }
            strAreaList = strTemp;
        }

        //DataGrid当选择项为空的处理提示函数
        private bool SelectItemIsNull(string str)
        {
            if (str == "" || str == null)
            {
                //Lpgl.LM.Business.Global.ShowMessage(ConstString.STR_NO_RECORD_SELECTED);
                return false;
            }
            else
                return true;
        }

        //选择同一城区复选框更改响应事件
        protected void SelectSameArea_CheckedChanged(object sender, System.EventArgs e)
        {
            for (int k = 0; k < ListArea.Items.Count; k++)
            {
                CheckBox ck1 = (CheckBox)ListArea.Items[k].Cells[0].FindControl("ctrl");

                if (ck1.Checked == true)
                {
                    CheckBox radb = (CheckBox)sender;
                    if (radb.Checked == true)
                    {
                        for (int i = 0; i < ListArea.Items.Count; i++)
                        {
                            string strTemp = ListArea.Items[i].Cells[3].Text;
                            strTemp = ListArea.Items[i].Cells[4].Text;
                            if (ListArea.Items[i].Cells[4].Text == ListArea.Items[k].Cells[4].Text)
                            {
                                CheckBox ck2 = (CheckBox)ListArea.Items[i].Cells[0].FindControl("ctrl");
                                ck2.Checked = true;
                            }
                        }
                    }
                    else
                    {
                        UpdateCurDepListArea();
                    }

                }
            }

        }

        //选择所有城区复选框更改响应事件
        protected void SelectAllArea_CheckedChanged(object sender, System.EventArgs e)
        {
            CheckBox radb = (CheckBox)sender;
            if (radb.Checked == true)
            {
                for (int i = 0; i < ListArea.Items.Count; i++)
                {
                    CheckBox ck1 = (CheckBox)ListArea.Items[i].Cells[0].FindControl("ctrl");
                    ck1.Checked = true;
                }
            }
            else
            {
                for (int i = 0; i < ListArea.Items.Count; i++)
                {
                    CheckBox ck1 = (CheckBox)ListArea.Items[i].Cells[0].FindControl("ctrl");
                    ck1.Checked = false;
                }
            }
        }

        //保存部门片区权限修改
        private void BtnSaveDep_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            //定义当前部门ID
            int nId = 0;
            //定义部门片区权限
            string strAreaList;
            //获取当前部门ID
            GetSelecteDep(out nId);
            //定义部门拥有权限的片区数组
            ArrayList naAreaId = new ArrayList();
            //取得部门片区权限
            GetSelListArea(ref naAreaId);
            //组合部门片区权限字符串
            ReconvertAreaList(out strAreaList, naAreaId);
            //更新片区到数据库
            SetDepAreaList(nId, strAreaList);

            //定义楼盘用途字符串
            string strUsage;
            //取得楼盘用途
            GetSelListUsage(out strUsage);
            //更新楼盘用途到数据库
            SetDepUsageList(nId, strUsage);

            //ListArea_SelectedIndexChanged(ListArea, null);
            //
            if (this.cbxAreaCust.Visible == true)
            {
                UpdateAreaCust(nId, this.cbxAreaCust.Checked);
            }
            else
            {
                UpdateAreaCust(nId, false);
            }

            _JavaScript.Alert("保存成功!");
        }

        //绑定可查看电话的豪宅
        private void BindLuxTelEstate(string eidlist)
        {
            hiddLuxTelEstateList.Value = eidlist;
            if (eidlist != string.Empty)
            {
                string strSql = "select Id,name ename from t_bi_Estate where Id in (" + eidlist + ") order by name";

                using (_Database db = new _Database("estate"))
                {
                    DataSet ds = db.Query(strSql);
                    this.rpLuxTelEstate.DataSource = ds.Tables[0].DefaultView;
                }
            }
            else
            {
                this.rpLuxTelEstate.DataSource = null;

            }
            this.rpLuxTelEstate.DataBind();
        }

        //删除可查看电话的豪宅
        protected void rpReportList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                string sTmp = "," + hiddLuxTelEstateList.Value + ",";
                string sTmp2 = "," + e.CommandArgument + ",";
                sTmp = sTmp.Replace(sTmp2, ",").TrimEnd(',').TrimStart(',');
                hiddLuxTelEstateList.Value = sTmp;
                BindLuxTelEstate(sTmp);
            }
        }

        //添加可查看电话的豪宅
        protected void btAddLuxTelEstate_Click(object sender, EventArgs e)
        {
            string strSql;
            DataSet ds;
            if (hiddLuxTelEstateList.Value.Trim() != string.Empty)
            {
                strSql = "select id from t_bi_estate where dltflag = 0 and id in (" + hiddLuxTelEstateList.Value + ") and name = " + (this.tbLuxTelEstate.Text.Trim());

                using (_Database db = new _Database("estate"))
                {
                    ds = db.Query(strSql);
                    this.rpLuxTelEstate.DataSource = ds.Tables[0].DefaultView;
                }

                if (ds.Tables[0].Rows.Count > 0)
                {
                    //已经添加
                    _JavaScript.Alert("该楼盘已经添加!");
                }
            }

            strSql = "select id from t_bi_estate where dltflag = 0 and name = " + this.tbLuxTelEstate.Text.Trim();

            using (_Database db = new _Database("estate"))
            {
                ds = db.Query(strSql);
                this.rpLuxTelEstate.DataSource = ds.Tables[0].DefaultView;
            }

            if (ds.Tables[0].Rows.Count > 0)
            {
                hiddLuxTelEstateList.Value += "," + ds.Tables[0].Rows[0][0].ToString();
                hiddLuxTelEstateList.Value = hiddLuxTelEstateList.Value.TrimStart(',');
                BindLuxTelEstate(hiddLuxTelEstateList.Value);
                this.tbLuxTelEstate.Text = string.Empty;
            }
            else
            {
                _JavaScript.Alert("输入的楼盘不存在!");
            }
        }

        // 非管理员  禁止删除可以查看电话的豪宅
        protected void rpLuxTelEstate_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            System.Web.UI.WebControls.LinkButton lbt = (LinkButton)e.Item.FindControl("ltlDel");
            // lbt.Visible = GetManager();
        }
        */

        /// <summary>
        /// 返回用途
        /// </summary>
        /// <param name="useStr"></param>
        /// <returns></returns>
        private static string ReturnUse(string useStr)
        {
            string useValue = "";
            if (useStr == "住宅")
                useValue = "1";
            else if (useStr == "商铺")
                useValue = "2";
            else if (useStr == "写字楼")
                useValue = "3";
            else if (useStr == "商住")
                useValue = "4";
            else if (useStr == "仓库")
                useValue = "5";
            else if (useStr == "厂房")
                useValue = "6";
            else if (useStr == "地皮")
                useValue = "7";
            else if (useStr == "项目盘")
                useValue = "8";
            else if (useStr == "其他*")
                useValue = "9";
            else if (useStr == "民房")
                useValue = "10";
            return useValue;
        }
    }
}
